<?php
/*
Plugin Name: Random Poetry
Plugin URI: http://mifunny.info/wp-plugin-random-poetry
Description: Random Show Tang Shi Song Ci
Version: 0.1
Author: LD King
Author URI: http://mifunny.info
*/

class LD_Poem {
	private $poem_src, $poem_file;
	private $poem; //Poem_Inter Object
	
	/**
	 * 选泽一个模块
	 *
	 * @param ArrayObject $files
	 */
	public function __construct($files = 
		array('tang300', 'song100', 'songproses')) 
	{	for ($i=0; $i<count($files); $i++) {
			if (!file_exists($files[$i])) {
				echo '<p>File '.$files[$i].' is not exists.</p>';
				exit;
			}				
		} //END for file_exists
		
		$rand_num = mt_rand(0, count($files)-1);
		$this->poem_file = $files[$rand_num];
		$this->poem_src = $this->get_poem($this->poem_file);
	} //END func __construct

	/**
	 * 从文件中随机抽取一首诗
	 *
	 * @param string $filename
	 * @return string
	 */
	private function get_poem($filename) {
		@ $fb = fopen($filename,'rb',TRUE); //open file
		if (!$fb) 	{
			echo "<p>I can't open this file: ".$filename."<p>";
			exit;
		}
		$n = 0; // the number of poem
		while (!feof($fb)) { //count poems
			$tang = fgets($fb, 999);
			if ( ereg('[%]', $tang) )
			$n++;
		}
		$rand_num = mt_rand(1, $n); //random number
		rewind($fb); //back to 0 node
		$begin = $end = 0; 
		for($num = 0; !feof($fb); ) {
			$tang = fgets($fb,999);
			if (ereg('[%]',$tang)) {
				$num++;
				if ($num == $rand_num ) {
					$end = ftell($fb);
					break; //get poem node
				}
				else
					$begin = ftell($fb);			 
			}
		} //END for feof($fb)
		$len=$end - $begin;
		fseek($fb, $begin, SEEK_SET);
		$result = fread($fb,$len);
		fclose($fb);  //close file

		return $result;
	} //END func get_poem_src	
	
	public function __get($name){
		return $this->$name;
	}

	/**
	 * 调用格式化接口，输出诗词
	 *
	 */
	public function display(Poem_Inter $poem) {
		$this->poem=$poem;
		echo $this->poem->format_poem($this->poem_src);
	} //END func display
} //END class LD_Poem	

/**
 * 诗词接口，便于类LD_Poem的调用
 *
 */
interface Poem_Inter {
	/**
	 * 把从文件中读取的字符串转换成HTML代码
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src);
} //END inter Poem_Inter


/**
 * 唐诗300首
 * tang300
 */
class tang300 implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split('<br />',nl2br($poem_src));
		
		$result = "<span class='title' >".$poem_arr[0]."</span><br />";
		$result = $result."<span class='author' >".$poem_arr[1]."</span><br />";
		$result = $result."<span class='content' >".$poem_arr[2];
		for ($n=3; $n <= count($poem_arr)-3; $n++ ) {
			$result = $result."<br />".$poem_arr[$n];
		}
		$result = $result."</span>";
		return ereg_replace(chr(10),'',$result);
	}	
} //END class tang300

/**
 * 宋诗100首
 * song100
 */
class song100 implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split('<br />',nl2br($poem_src));
		
		$result = "<span class='title' >".$poem_arr[0]."</span><br />";
		$result = $result."<span class='author' >".$poem_arr[1]."</span><br />";
		$result = $result."<span class='content' >".$poem_arr[2];
		for ($n=3; $n <= count($poem_arr)-3; $n++ ) {
			$result = $result."<br />".$poem_arr[$n];
		}
		$result = $result."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class song100

/**
 * 宋词
 * songproses
 */
class songproses implements Poem_Inter {
	/**
	 * @see Poem_Inter::format_poem()
	 *
	 * @param string $poem_src
	 */
	public function format_poem($poem_src) {
		$poem_arr = split('<br />',nl2br($poem_src));
		$result = "<span class='content' >";
		for ($n=0; $n <= count($poem_arr)-4; $n++)
			$result .= $poem_arr[$n]."<br />";
		$poem_at = split('@',$poem_arr[count($poem_arr)-3]);
		$result .= "</span><span class='author' >".$poem_at[0]
			."</span>   <span class='title' >".$poem_at[1]."</span>";
		return ereg_replace(chr(10),'',$result);
	} //END func format_poem
} //END class format_poem

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>随机显示唐诗300首之一[面向对象方法]</title>
<link rel="stylesheet" href="poem.css" />
<style type="text/css">
.over
{
height: 150px;
width: 300px;
overflow: scroll;
overflow-x: auto;/*hidden;*/
overflow-y: auto;
}
</style>
</head>
<body>
<?php
echo "我实在想知道真相！<br />";
$test = new LD_Poem();
echo "如果不知道就不要说！<br />";
echo $test->poem_file;
?>
<hr /><div class='over' >
<?php echo $test->display(new $test->poem_file); ?>
</div>
<hr />
</body>
</html>
